package com.rms.controller;

import com.alibaba.fastjson.JSONObject;
import com.rms.entity.Admin;
import com.rms.service.AdminService;
import com.rms.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 胡满意
 * @date 2019/10/24
 */
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/member/login")
    public JSONObject login(String adminName,String adminPassword){
        JSONObject object = new JSONObject();
        try {
            Admin admin = adminService.login(adminName);
            if (adminPassword.equals(admin.getAdminPassword())){
                TokenUtil tokenUtil = new TokenUtil();
                String token = tokenUtil.getToken(String.valueOf(admin.getAdminId()),admin.getAdminPassword());
                object.put("state","success");
                object.put("token",token);
                object.put("message","用户登录成功");
                return object;
            }else {
                object.put("state","error");
                object.put("message","用户名或密码错误");
                return object;
            }
        }catch (Exception e){
            e.printStackTrace();
            object.put("state","error");
            object.put("message","用户登录失败");
            return object;
        }
    }
}
